import UIKit
import SwiftUI
import MobileCoreServices
import QuickLook
import Foundation




func fileTypeInt(from fileURL: String) -> Int {
    guard let fileExtension = fileURL.split(separator: ".").last?.lowercased() else {
        return -1
    }
    switch fileExtension {
    case "pdf":
        return 0
    case "html":
        return 1
    case "excel":
        return 2
    case "txt":
        return 3
    case "docx":
        return 4
    case "ppt":
        return 5
    default:
        return -1
    }
}

func fileTypeString(from intValue: Int) -> String {
    switch intValue {
    case 0:
        return "pdf"
    case 1:
        return "html"
    case 2:
        return "excel"
    case 3:
        return "txt"
    case 4:
        return "docx"
    case 5:
        return "ppt"
    default:
        return "unknown"
    }
}


func utTypes(for fileType: Int) -> [UTType] {
    let types: [FileType: UTType] = [
           .PDF: .pdf,
           .EXCEL: .spreadsheet,
           .HTML: .html,
           .PPT: .presentation,
           .TXT: .plainText,
           .WORD: UTType(filenameExtension: "docx") ?? .plainText

       ]
    
    guard let fileType = FileType(rawValue: fileType), let utType = types[fileType] else {
        return []
    }
    
    return [utType]
}

func getFileConversionTypes(isFromPDF: Bool, fileType: String) -> [Int] {
    let targetType: FileType?
    
    switch fileType.uppercased() {
    case "PDF":
        targetType = .PDF
    case "EXCEL":
        targetType = .EXCEL
    case "WORD":
        targetType = .WORD
    case "HTML":
        targetType = .HTML
    case "TXT":
        targetType = .TXT
    case "PPT":
        targetType = .PPT
    default:
        targetType = nil
    }
    
    if let targetType = targetType {
        if isFromPDF {
            return [targetType.rawValue, FileType.PDF.rawValue]

        } else {
            return [FileType.PDF.rawValue, targetType.rawValue]
        }
    } else {
        return []
    }
}


enum FileType: Int {
    case PDF = 0
    case HTML = 1
    case PPT = 2
    case TXT = 3
    case WORD = 4
    case EXCEL = 5
    
    var description: String {
        switch self {
        case .PDF:
            return "PDF"
        case .EXCEL:
            return "EXCEL"
        case .WORD:
            return "WORD"
        case .TXT:
            return "TXT"
        case .HTML:
            return "HTML"
        case .PPT:
            return "PPT"
        }
    }
}



struct APIResponse: Codable {
    let code: Int
    let msg: String?
    let result: String
}


let toPdfList = [
    PdfItem(text: "EXCEL", imageName: "excel"),
    PdfItem(text: "HTML", imageName: "html"),
    PdfItem(text: "PPT", imageName: "ppt"),
    PdfItem(text: "TXT", imageName: "txt"),
    PdfItem(text: "WORD", imageName: "word")
]


let pdfToList = [
    
    
    PdfItem(text: "TXT", imageName: "txt"),
  
    PdfItem(text: "HTML", imageName: "html"),
    
    PdfItem(text: "EXCEL", imageName: "excel"),
    
    PdfItem(text: "WORD", imageName: "word"),
    
    PdfItem(text: "PPT", imageName: "ppt")
 
   
]



let columns = [
    GridItem(.flexible(), alignment: .leading),
    GridItem(.flexible(), alignment: .leading),
    GridItem(.flexible(), alignment: .leading)
]





import Foundation
import SwiftUI
import GoogleMobileAds
private let adShowCountKey = "adShowCount"
private let adAllShowCountKey = "adAllShowCount"
private let lastDateKey = "lastDate"
private let allAdShowCountsKey = "allAdShowCounts"
private let installationDateKey = "installationDate"
let uuidKey = "uniqueDeviceIdentifier"
private let calendar = Calendar.current


//日志uuid
func getEventId() -> String {
    let uuid = UUID()
    return uuid.uuidString
}

func logAd(type:String,adId: String,value:String,precision:String,currencyCode:String,adUnitId:String){
    print("xianban:*****************\(type)广告***************************")
    print("xianban: 广告来源 ID: \(adId)") // 输出广告来源 ID
    print("xianban: 广告价值: \(value)") // 输出广告价值
    print("xianban: 精度: \(precision)") // 输出广告收入的精度
    print("xianban: 货币代码: \(currencyCode)") // 输出货币代码
    print("xianban: 广告单元 ID: \(adUnitId)") // 输出广告单元 ID

}



//上传日志接口

func logData(position: String, status: String, uid: String, info: String, currency: String, cicros: String,errorInfo:String = "",deepLink:String = "") {
    let entry = LogEntry(position: position, status: status, uid: uid, info: info, currency: currency, cicros: cicros,errorinfo: errorInfo, refer: deepLink)
    do {
        // 直接编码LogEntry实例为JSON Data
        let jsonData = try JSONEncoder().encode(entry)
        if let jsonString = String(data: jsonData, encoding: .utf8) {
       
            
            print(jsonString,"\n \n")
            
            if let aesUtil = AESUtil() {
             
                if let encryptedString = aesUtil.encrypt(jsonString) {
                    
                    print("jiamishuju",encryptedString)
                    let networkUtil = NetworkUtil.shared
                    networkUtil.postRequest(urlString: logUrl, parameters: encryptedString) { result in
                        switch result {
                        case .success(let data):
                            if let responseString = String(data: data, encoding: .utf8) {
                                       print("POST 请求成功: \(responseString)")
                                   } else {
                                       print("POST 请求成功，但无法转换数据为字符串")
                                   }
                        case .failure(let error):
                            print("POST 请求失败: \(error)")
                        }
                    }

                }
            }
        } else {
            print("Failed to convert JSON data to string.")
        }
    } catch {
        print("Error encoding LogEntry to JSON: \(error.localizedDescription)")
    }
}


//获取IOS版本
func getiOSVersion() -> String {
        return UIDevice.current.systemVersion
    }

//获取APP版本
func getAppVersion() -> String? {
        return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
    }



func logUtil(logString: String){
    print("xianban:\(logString)")
}



func getUserId() -> String {
    if let uuid = UserDefaultsManager.shared.getString(forKey: uuidKey), uuid != "null" {
        return uuid
    } else {
       
        let newUUID = UUID().uuidString
      
        UserDefaultsManager.shared.setString(newUUID, forKey: uuidKey)
        return newUUID
    }
}




func getColumns() -> [GridItem]{
    
    return [
        GridItem(.flexible()),  GridItem(.flexible()),  GridItem(.flexible()),
           GridItem(.flexible())
       ]
    
}

func getAppColumns() -> [GridItem]{
    
    return [
        GridItem(.flexible()),  GridItem(.flexible())
       ]
    
}
func nameToLowercase(_ input: String) -> String {
    return input.lowercased()
}


